package com.guyan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guyan.entity.Product;
import com.guyan.mapper.ProductMapper;
import com.guyan.service.ProductService;
import com.guyan.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * ClassName： ProductServiceImpl
 * Date： 2021/5/24 15:38
 *
 * @author 孤焰
 * @version 1.0
 **/

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Override
    public PageInfo productList(Integer page, Integer limit, Integer categoryId, String search) {
        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(productMapper.productList(categoryId, search));
        return pageInfo;
    }

    @Override
    public List<Product> productListNoPage() {
        return productMapper.productList(null, null);
    }

    @Override
    public Product getProductById(Integer id) {
        return productMapper.getProductById(id);
    }

    @Override
    public ResultVo updateProductById(Product product) {
        product.setUpdateTime(new Date());
        int row = productMapper.updateProductById(product);
        return row > 0 ? ResultVo.success() : ResultVo.error(6000, "更新信息失败");
    }

    @Override
    public ResultVo addProduct(Product product) {
        Date date = new Date();
        product.setUpdateTime(date);
        product.setCreateTime(date);

        int row = productMapper.addProduct(product);
        return row > 0 ? ResultVo.success() : ResultVo.error(6000, product + "数据插入失败");
    }

    @Override
    public ResultVo batchDelProduct(Integer[] ids) {
        return productMapper.batchDelProduct(ids) > 0 ?
                ResultVo.success() : ResultVo.error(6000, "数据批量删除失败");
    }
}
